import colorModule from './modules/color.js'
import regModule from './modules/regexValidate.js'
import resizeModule from './modules/eleResize.js'
import mapChina from './modules/map-china.json'

export let color = colorModule;
export let regexValidate = regModule;
export let eleResize = resizeModule;
export let mapChinaJson = mapChina;

/* 生成随机uuid
*	n：uuid的长度
* type：类型（0-大小写字母+数字，1-小写字母+数字，2-纯数字，3-大小写字母，4-小写字母）
*/
export function uuid(n=6,type=0){
	let res='', uType=0;
	const __uuidLetter = function(){
		const __tempArr = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
		return __tempArr[Math.floor(Math.random()*26)];
	}
	for (let i=0;i<n;i++) {
		switch(type) {
			case 1:
				uType = Math.floor(Math.random()*2);
				if (uType===0) {
					res += Math.floor(Math.random()*10).toString()
				} else {
					res += __uuidLetter()
				}
				break;
			case 2:
				res += Math.floor(Math.random()*10).toString()
				break;
			case 3:
				uType = Math.floor(Math.random()*2);
				if (uType===0) {
					res += __uuidLetter()
				} else {
					res += __uuidLetter().toUpperCase()
				}
				break;
			case 4:
				res += __uuidLetter()
				break;
			default:
				uType = Math.floor(Math.random()*3);
				if (uType===0) {
					res += Math.floor(Math.random()*10).toString()
				} else if (uType===1) {
					res += __uuidLetter()
				} else {
					res += __uuidLetter().toUpperCase()
				}
				break;
		}
	}
	return res;
}

/* 数字格式化，前面补足 */
export function numberFormat(val,fix=2,replaceChar="0") {
	if (typeof val === 'number') {
		val = val.toString();
	}
	if (typeof val !== 'string') {
		return val;
	}
	let len = val.length, newVal = val;
	if (len<fix) {
		for (let i=len;i<fix;i++){
			newVal = replaceChar + newVal;
		}
	} 
	return newVal
}

/* 千分位格式化 */
export function thousandsFormat(val,thousandChar=',') {
	if (typeof val === 'number') {
		val = val.toString();
	}
	if (typeof val !== 'string') {
		return 'Invalid Data';
	}
	let res = '',c=0;
	for (let i=val.length-1;i>=0;i--){
		if (c%3===0 && c!==0) {
			res = thousandChar + res;
		}
		res = val.substr(i,1) + res
		c++
	}
	return res;
}


/* 顶部提示信息 */
const __message = function () {
    const _id = 'page_message';
    let _res = {}
		const _show = function(msg,type='info') {
			/* 创建信息dom */
			let $icon = window.document.createElement('span')
			let $messagebox = window.document.createElement('span')
			$icon.style.marginRight = '6px'
			$messagebox.style.padding = '6px 12px'
			$messagebox.style.borderRadius = '3px'
			$messagebox.style.display = 'flex'
			$messagebox.style.alignItems = 'center'
			$messagebox.style.marginBottom = '4px'
			if (type==='success') {
				$icon.innerHTML = '&#9786;'
				$icon.style.fontSize = '1.4em'
				$messagebox.style.backgroundColor = '#f0f9eb';
				$messagebox.style.color = '#67c23a';
			} else if (type==='error') {
				$icon.innerHTML = '&#9938;'
				$messagebox.style.backgroundColor = '#fef0f0';
				$messagebox.style.color = '#f56c6c';
			} else {
				$icon.innerHTML = '&#9888;'
				if (type==='warning') {
					$messagebox.style.backgroundColor = '#fdf6ec';
					$messagebox.style.color = '#e6a23c';
				} else {
					$messagebox.style.backgroundColor = '#f4f4f5';
					$messagebox.style.color = '#909399';
				}
			}
			let $message = window.document.createElement('span')
			$message.innerText = msg;
			$messagebox.appendChild($icon)
			$messagebox.appendChild($message)
			/* 创建弹出层 */
			let $dom = window.document.getElementById(_id)
			if ($dom) {
				$dom.style.display="flex"
			} else {
				$dom = window.document.createElement('div')
				$dom.id = _id
				$dom.style.display = 'flex';
				$dom.style.zIndex = '999999';
				$dom.style.alignItems = 'center';
				$dom.style.flexDirection = 'column';
				$dom.style.position = 'fixed';
				$dom.style.top = '5vh';
				$dom.style.left = '0';
				$dom.style.width = '100%';
				$dom.style.fontSize = '14px';
				$dom.style.backgroundColor = 'transparent';
				const $body = window.document.getElementsByTagName('body')[0];
				$body.appendChild($dom)
			}
			/* 将信息dom加载到弹出层 */
			$dom.appendChild($messagebox)
			window.setTimeout(function(){
				$messagebox.remove()
			},3000)
		}
    _res.warning = function(msg){
    	_show(msg,'warning')
    }
		_res.error = function(msg){
			_show(msg,'error')
		}
		_res.success = function(msg){
			_show(msg,'success')
		}
		_res.info = function(msg){
			_show(msg)
		}
    return _res;
}();
export let $message = __message


/* 顶部提示信息 */
const __loading = function () {
    const _id = 'page_loading';
    let _res = {}
    _res.show = function(){
    	let $dom = window.document.getElementById(_id)
    	if ($dom) {
    		$dom.style.display="flex"
    	} else {
				$dom = window.document.createElement('div')
				$dom.id = _id
				$dom.style.display = 'flex';
				$dom.style.position = 'absolute';
				$dom.style.zIndex = '99999999999999999';
				$dom.style.width = '100%';
				$dom.style.height = '100%';
				$dom.style.top = '0';
				$dom.style.left = '0';
				$dom.style.backgroundColor = 'transparent';
				$dom.style.color = '#00ffff';
				$dom.style.alignItems = 'center';
				$dom.style.justifyContent = 'center';
				$dom.style.fontSize = '32px';
				$dom.innerHTML = '<div class="loadEffect"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></div><div>加载中...</div>'
				const $body = window.document.getElementsByTagName('body')[0];
				$body.appendChild($dom)
			}
    }
		_res.hide = function(msg){
			let $dom = window.document.getElementById(_id)
			try{
				$dom.style.display = 'none';
			}catch(e){
				//TODO handle the exception
			}
		}
    return _res;
}();
export let $loading = __loading